package com.meta.context.source.http.util;

import cn.hutool.core.text.UnicodeUtil;
import com.alibaba.fastjson2.JSONValidator;
import com.alibaba.fastjson2.schema.JSONSchema;
import lombok.SneakyThrows;

import java.util.HashMap;

/**
 * @author lwh
 */
public class RequestUtil {


    /**
     * 第三方接口调用方式
     *
     * @param url       请求地址
     * @param param     请求参数
     * @param way       请求方式 get post
     * @param headerMap 请求头 headerMap
     * @return String
     */
    @SneakyThrows
    public static String requestApi(String url, HashMap<String, Object> param, String way, HashMap<String, String> headerMap) {
        HttpClientResult httpClientResult = null;

        try {
            if (MateConstant.GET.equals(way)) {
                httpClientResult = HttpUtils.doGet(url, headerMap, param, null, null);
            } else if (MateConstant.POST.equals(way)) {
                httpClientResult = HttpUtils.doPost(url, headerMap, param, null, null);
            } else if (MateConstant.POST_FORM.equals(way)) {
                httpClientResult = HttpUtils.doPostForm(url, headerMap, param, null, null);
            } else if (MateConstant.BODY.equals(way)) {
                httpClientResult = HttpUtils.doPostJson(url, headerMap, param, null, null);
            } else if (MateConstant.DELETE.equals(way)) {
                httpClientResult = HttpUtils.doDeleteJson(url, headerMap, param);
            } else if (MateConstant.PUT.equals(way)) {
                httpClientResult = HttpUtils.doPut(url, headerMap, param, null, null);
            } else if (MateConstant.PUT_JSON.equals(way)) {
                httpClientResult = HttpUtils.doPutJson(url, headerMap, param);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //对返回数据进行解析
        assert httpClientResult != null;
        return UnicodeUtil.toString(httpClientResult.getContent());
    }

    public static void main(String[] args) {
        HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("X-API-KEY", "edd1c9f034335f136f87ad84b625c8f1");
        String url="http://localhost:9180/apisix/admin/plugins/list";
        String routesStr = RequestUtil.requestApi(url, null, MateConstant.GET, headerMap);
        System.out.println(routesStr);
         url="http://localhost:9180/apisix/admin/plugins/fault-injection?subsystem=http";
        //fault-injection
        routesStr = RequestUtil.requestApi(url, null, MateConstant.GET, headerMap);

        System.out.println(routesStr);
        String JSON_SCHEMA = "{\n" +
                "    \"type\":\"object\",\n" +
                "    \"properties\":{\n" +
                "        \"name\":{\n" +
                "            \"type\":\"string\"\n" +
                "        },\n" +
                "        \"age\":{\n" +
                "            \"type\":\"integer\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        JSONValidator jsonValidator =   JSONValidator.from(JSON_SCHEMA);
//        jsonValidator.validate(routesStr);

    }
}
